//
//  SortDetailViewController.m
//  QuicklyPlay
//
//  Created by 你的温柔是我唯一沉溺 on 15/11/2.
//  Copyright © 2015年 OPteam. All rights reserved.
//

#import "SortDetailViewController.h"
#import "SortListSecondCell.h"
#import "SortDetailHelper.h"
#import "HotsDetailViewController.h"
#import "GestureRecognizerTool.h"


@interface SortDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *sortTableView;
@property (strong, nonatomic) SortDetailHelper *sortDetailHelper;
@property (strong, nonatomic) NetworkHelper *networkHelper;

@property (weak, nonatomic) IBOutlet UIImageView *fuzzyImageView;

@property (weak, nonatomic) IBOutlet UIImageView *headerImageView;

@property (weak, nonatomic) IBOutlet UILabel *videosNum;

@property (weak, nonatomic) IBOutlet UILabel *digs;

@property (assign, nonatomic) NSInteger index;

@property (assign, nonatomic) NSInteger page;

@property (strong, nonatomic) NSMutableArray *reciveArray;

@property (weak, nonatomic) IBOutlet UINavigationItem *titleItem;

@end

@implementation SortDetailViewController


- (NetworkHelper *)networkHelper {
    if (!_networkHelper) {
        _networkHelper = [NetworkHelper sharedInstance];
    }
    return _networkHelper;
}

- (SortDetailHelper *)sortDetailHelper {
    if (!_sortDetailHelper) {
        _sortDetailHelper = [SortDetailHelper sharedInstance];
    }
    return _sortDetailHelper;
}


-(NSMutableArray *)reciveArray {
    if (!_reciveArray) {
        _reciveArray = [NSMutableArray array];
    }
    return _reciveArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];

    [self creatFuzzy];
    [self updateUI];
    self.page = 0;
    
//    注册Cell
    [self.sortTableView registerNib:[UINib nibWithNibName:@"SortListSecondCell" bundle:nil] forCellReuseIdentifier:@"sortlist"];
    [self.sortTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    

    
//    设置title
    self.titleItem.title= [NSString stringWithFormat:@"%@", self.reciveModel.name];

    [self loadMoreData];
    
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    self.sortTableView.footer = footer;

    
    [[GestureRecognizerTool creatScreenEdgePanGestureRecognizerLeftWithViewController:self] addTarget:self action:@selector(backItem:)];
    
    [self creatFuzzy];

}






//创建毛玻璃背景
- (void)creatFuzzy {
    
    CIContext *context = [CIContext contextWithOptions:nil];

    
    CIImage *inputImage = [[CIImage alloc] initWithImage:[UIImage imageNamed:@"233333.jpg"]];
    // create gaussian blur filter
    CIFilter *filter = [CIFilter filterWithName:@"CIGaussianBlur"];
    [filter setValue:inputImage forKey:kCIInputImageKey];
    [filter setValue:[NSNumber numberWithFloat:5.0] forKey:@"inputRadius"];
    // blur image
    CIImage *result = [filter valueForKey:kCIOutputImageKey];
    CGImageRef cgImage = [context createCGImage:result fromRect:[result extent]];
    UIImage *image = [UIImage imageWithCGImage:cgImage];
    CGImageRelease(cgImage);
    
    self.fuzzyImageView.image = image;
    
    
}







- (void)updateUI {
    
    [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:self.reciveModel.img_url]];
    
    NSString *Videos = [NSString stringWithFormat:@"%@个视频", self.reciveModel.videos];
    NSString *Digs = [NSString stringWithFormat:@"%@个人赞过哦",self.reciveModel.digs];

    self.digs.text = Digs;
    self.videosNum.text = Videos;
}





- (void)loadMoreData {
    
    self.page += 1;
    
    NSLog(@"%ld", self.page);
    
    //    获取数据源
    NSString *urlString  = [self.sortDetailHelper refreshAndDownloadNewDataWithAID:self.reciveModel.album_id WithPages:[NSString stringWithFormat:@"%ld", (long)self.page]];
    
    [self loadDataWithURL:urlString];
    
    
}



- (void) loadDataWithURL:(NSString *)url {
    [NetworkHelper getMoreDataWithURL:url WithRequestAllDataDidFinishSuccess:^(id Array) {
        
        //接收回调出来的数组 并通过MJExtension 转换成模型
        NSArray *reciveArr = [VideosModel objectArrayWithKeyValuesArray:Array];
        
        //接收媒介数组
        NSMutableArray *temp=[NSMutableArray array];
        
        for (VideosModel *newModel in reciveArr) {
            VideosModel *model = [[VideosModel alloc] init];
            model = newModel;
            //媒介数组存储数据model
            [temp addObject:model];
        }
        
//        接收每次请求下来的媒介数组
        [self.reciveArray addObjectsFromArray:temp];
        
//        判断数据是否加载完毕
        if (temp.count < 20) {
            [self.sortTableView.footer endRefreshingWithNoMoreData];
          
            [self.sortTableView reloadData];

        } else {
               
            [self.sortTableView.footer endRefreshing];
            [self.sortTableView reloadData];

        }

    } failture:^(id error) {
        [self.sortTableView.footer endRefreshingWithNoMoreData];
        NSLog(@"%@", error);
    }];
    

}






- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
    //NSLog(@"%lu",(unsigned long)self.reciveArray.count);
    
    return self.reciveArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        SortListSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"sortlist" forIndexPath:indexPath];
        VideosModel *model = self.reciveArray[indexPath.row];
        [cell setVideoModel:model];
        UIView *backView = [[UIView alloc] initWithFrame:cell.frame];
        cell.selectedBackgroundView = backView;
        cell.selectedBackgroundView.backgroundColor = [UIColor clearColor];
        return cell;

}




//跳转播放详细页面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([NetworkHelper judgeNetworkStatus]) {
        VideosModel *model = self.reciveArray[indexPath.row];
        HotsDetailViewController *hostDetailVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HotsDetailVC"];
        //将model的内容添加到要跳转到的页面的model上面:
        hostDetailVC.reModel = model;
        
        [self presentViewController:hostDetailVC animated:YES completion:nil];

    }

    
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        return 100;
}







- (IBAction)backItem:(UIBarButtonItem *)sender {

    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
